package com.wise.service.cms.content.setting;

import com.wise.entities.content.Setting;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.session.CurrentUserId;
import com.wise.framework.validation.Precondition;
import com.wise.model.adminuser.Menus;
import com.wise.summer.v1.cms.adminuser.usermenu.GetUserMenuById;
import com.wise.summer.v1.cms.content.setting.GetSetting;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.ArrayList;
import java.util.List;

import static com.wise.model.content.ContentErrMsgs.Err_000100010001;


/**
 * 加载系统Json信息 - 如menus
 * <p>
 * updated by futao on 2019/06/04 - 添加注释
 *
 * @author futao
 */
@Service
@SummerService
public class GetSettingService implements SummerServiceBean<GetSetting> {
    @Override
    public void sum(GetSetting model) {
//        String userId = CurrentUserId.s(true);
//        GetUserMenuById getUserMenuById = new GetUserMenuById();
//        getUserMenuById.setId(userId);
//        List<Menus> menusList = getUserMenuById.sum();
        boolean b = hasAuth();

        Precondition.checkState(StringUtils.isNotBlank(model.getName()), this.getClass(), Err_000100010001);
        SqlSelect select = SqlSelect.selec("*");
        select.fromClassName(Setting.class);
        select.eq(StringUtils.isNotBlank(model.getName()), Setting._name, model.getName());
        select.eqLang(Setting._lang);
        List<Setting> infoList = DaoList.s(Setting.class, select);
        if (infoList.size() > 0) {
            model.setSummerResult(infoList.get(0).getInfo());
        }
    }
    public boolean hasAuth() {
        String userId = CurrentUserId.s(true);
        GetUserMenuById getUserMenuById = new GetUserMenuById();
        getUserMenuById.setId(userId);
        List<Menus> menusList = getUserMenuById.sum();
        ArrayList<String> paths = new ArrayList<>();
        getPath(menusList, paths);
        return false;
    }

    private List<String> getPath(List<Menus> menusList, List<String> paths) {
        if (CollectionUtils.isEmpty(menusList)) {
            return null;
        }
        for (Menus menus : menusList) {
            if (StringUtils.isNotBlank(menus.getPath())) {
                paths.add(menus.getPath());
            }
            getPath(menus.getChildren(), paths);
        }
        return paths;

    }


}
